﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;


namespace WpfApplication1
{
    class HostsHandler
    {
        String path;
        public HostsHandler()
        {
            path = "C:\\Windows\\System32\\Drivers\\etc\\hosts";
        }

        public bool fuckHost(String host)
        {
            try
            {
                StreamWriter writer = File.AppendText(path);
                writer.WriteLine("1.2.1.2 " + host);
                writer.WriteLine("1.2.1.2 www." + host); 
                writer.Close();
                return true;
            }
            catch (Exception e)
            {
                return false;
            }

        }
        public bool fixHost(String host)
        {
            String[] lineas = File.ReadAllLines(path);
            String[] nuevaLineas = new String[lineas.Length - 1];
            int i = 0;
            foreach (String linea in lineas)
            {
                if (linea.Contains(host) != true)
                {
                    // Copy
                    nuevaLineas[i++] = linea;
                }

            }
            StreamWriter writer = new StreamWriter(path, false);
            foreach (String linea in nuevaLineas)
            {
                writer.WriteLine(linea);
            }
            writer.Close();
            return true;
        }
    }
}
